<?php
/**
 * Created by PhpStorm.
 * User: BlueIdeaLirui
 * Date: 2020/12/28
 * Time: 14:17
 */

namespace business\company\services;

use business\company\repositories\SelectionSupplierRepository;
use yii\base\InvalidConfigException;
use yii\db\Query;
use function GuzzleHttp\Psr7\str;

class SelectionSupplierService
{

    /**
     * 是否是精选供应商
     * @param $supplierId
     * @return bool
     * @throws InvalidConfigException
     */
    public function IsBoutique($supplierId)
    {
        $sql = <<<sql
                  select * from 
                     `b2b_supplier`.`selection_categorical_info` 
                  where supplier_id = :supplier_id AND is_deleted = 1
sql;
        $data =  \Yii::$app->get('dbSupplier')
            ->createCommand($sql)
            ->bindValue(':supplier_id', $supplierId)
            ->queryOne();
        if (empty($data)) {
            return false;
        }
        return true;
    }

    /**
     * 获取战略合作数量
     * @param $supplierId
     * @return int
     * @throws InvalidConfigException
     */
    public function getStrategicCaseNum($supplierId){
        $connection = \Yii::$app->get('dbSupplier');
        $sql = "select count(*) as num from `b2b_supplier`.`supplier_project` 
                where `supplier_id` = :supplier_id and  `project_type` = 1 
                and `is_deleted` = 1 and `audit_status` = 2";
        return (int)$connection->createCommand($sql)
            ->bindValue(':supplier_id', $supplierId)
            ->queryScalar();
    }


    /**
     * 获取供应商营业额
     * @param $supplierId
     * @return int
     * @throws InvalidConfigException
     */
    public function getSupplierRevenue($supplierId){
        $connection = \Yii::$app->get('dbSupplier');
        $sql = "select total from `b2b_supplier`.`supplier_revenue_detail` 
                where `supplier_id` = :supplier_id and  `audit_status` = 2
                and total != 0 
                order by year desc limit 1" ;
        $res = $connection->createCommand($sql)
            ->bindValue(':supplier_id', $supplierId)
            ->queryScalar();
        return (int)$res;
    }

    /**
     * 获取供应商绿建产品数量
     * @param $supplierId
     * @return int
     * @throws InvalidConfigException
     */
    public function getSupplierGreenProductNum($supplierId): int
    {
        $num  = 0;
        $connection = \Yii::$app->get('dbSupplier');
        $sql = "select publish_year,green_building_product_certification from `b2b_supplier`.`supplier_product` 
                where `supplier_id` = :supplier_id and  `audit_status` = 2
                and is_deleted = 1" ;
        $res = $connection->createCommand($sql)
            ->bindValue(':supplier_id', $supplierId)
            ->queryAll();
        if ($res){
            foreach ($res as $val) {
                if (!empty($val['green_building_product_certification'])) {
                    ++$num;
                }
            }
        }
        return $num;
    }

    public function getMySupplierCount($supplierIds, $developerId)
    {
        $str =  "('".implode("','",$supplierIds)."')";
        // 1.0 入库
        $sql = "SELECT
			count(1) as n,supplier_id
		FROM
			b2b_company.bid_developer_mysupplier tm
		WHERE supplier_id in $str and status = 2 and is_del = 0
        group by supplier_id";
        $query = \Yii::$app->db->createCommand($sql);
        $res = $query->queryAll();
        return $this->arrayChangeKey($res, "supplier_id", $developerId);
    }

    private function arrayChangeKey($arr,$key, $developerId)
    {
        $processedArr = $data = array();
        if(is_array($arr) && !empty($arr)){
            foreach ($arr as $item) {
                $processedArr['in_stock_count'] = $item['n'];
                $processedArr['is_storage'] = $this->isStorage($item[$key], $developerId);
                $processedArr['supplier_id'] = $item[$key];
                $data[] = $processedArr;
            }
        }
        return $data;
    }

    public function isStorage($supplierId, $developerId): bool
    {
        if (empty($developerId)){
            return false;
        }
        $sqlParams[':supplier_id'] = $supplierId;
        $sqlParams[':developer_id'] = $developerId;
        // 1.0 入库
        $sql1=<<<sql
        SELECT
			*
		FROM
			b2b_company.bid_developer_mysupplier tm
		WHERE supplier_id = :supplier_id and status = 2 and company_id = :developer_id
        group by supplier_id
sql;
        $query = \Yii::$app->db->createCommand($sql1, $sqlParams);
        $res1 = $query->queryOne();
        if ($res1){
            return true;
        }

        // ai-card实例
        $connection = \Yii::$app->get('dbSupplier');
        $sql2=<<<sql
        SELECT
			*
		FROM
			b2b_developer_mysupplier.developer_mysupplier
		WHERE supplier_id = :supplier_id and is_deleted = 1 and developer_id = :developer_id
        group by supplier_id
sql;
        $query = $connection->createCommand($sql2, $sqlParams);
        $res2 = $query->queryOne();
        if ($res2){
            return true;
        }
        return false;
    }

}
